﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Mail;

namespace BLL
{
    public class ePioneer_UserInfo
    {
        private readonly DAL.ePioneer_UserInfo dal = new DAL.ePioneer_UserInfo();
        public ePioneer_UserInfo()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.ePioneer_UserInfo model)
        {
            model.PassWord = LYM.CommonWeb.DecryptEncrypt.DESEncrypt.Encrypt(model.PassWord);
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ePioneer_UserInfo model)
        {
            model.PassWord = LYM.CommonWeb.DecryptEncrypt.DESEncrypt.Encrypt(model.PassWord);
            return dal.Update(model);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ePioneer_UserInfo GetModel(int Id)
        {
            return dal.GetModel(Id);
        }

        #endregion  Method

        #region 自定义方法
        /// <summary>
        /// 注册，返回值0：注册成功！1：用户名已经存在；2：注册失败
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Register(Model.ePioneer_UserInfo model)
        {
            Model.ePioneer_UserInfo m = dal.Login(model.LoginName);
            if (m != null)
                return "1";
            else
            {
                int r = Add(model);
                if (r > 0)
                    return "0";
                else
                    return "2";
            }
        }

        /// <summary>
        /// 登录，返回值0:登录成功！1:用户名不存在，请先注册！2:密码错误！
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>0:登录成功！1:用户名不存在，请先注册！2:密码错误！</returns>
        public string Login(string loginName, string password)
        {
            password = LYM.CommonWeb.DecryptEncrypt.DESEncrypt.Encrypt(password);
            Model.ePioneer_UserInfo model = dal.Login(loginName);
            if (model == null)
                return "1";
            else if (model.PassWord == password)
                return "0";
            else
                return "2";
        }

        /// <summary>
        /// 忘记密码并发送加密邮件，返回值0：成功1：用户名不存在
        /// </summary>
        /// <param name="loginName">未加密的用户名</param>
        /// <returns>0：成功1：用户名不存在</returns>
        public string ForgetPassWord(string loginName, string htmlBody)
        {
            Model.ePioneer_UserInfo model = dal.Login(loginName);
            if (model == null)
                return "1";
            else
            {
                loginName = LYM.CommonWeb.DecryptEncrypt.DESEncrypt.Encrypt(loginName);
                string url =LYM.CommonWeb.ConfigHelper.GetConfigString("ResetPWURL") + "?uid=" + loginName;
                string showName = string.IsNullOrEmpty(model.UserName) ? model.LoginName : model.UserName;
                string[] strOld = { "<#UserName>", "<#URL>" };
                string[] strNew = { showName, url };
                //发邮件
                string content = CreateEmail(htmlBody, strOld, strNew);
                string port = "25";
                string server = "mail.emstock.com.cn";
                string UserName = "dp@emstock.com.cn";
                string password = "1qaz@WSX";
                LYM.CommonWeb.SocketClient client = new LYM.CommonWeb.SocketClient();
                var col = new Dictionary<string, string>();
                col.Add("action", "SendMail");
                col.Add("email", model.LoginName);
                col.Add("port", port);
                col.Add("server", server);
                col.Add("UserName", UserName);
                col.Add("password", password);
                col.Add("title", "找回密码");
                col.Add("content", content);
                try
                {
                    SendEmail(model.LoginName, content);
                    //client.UpdateData(Common.ConfigHelper.GetConfigString("SendEmail"), col);
                }
                catch
                {
                    return "-1";
                }
                return "0";
            }
        }

        private void SendEmail(string to, string body)
        {
            SmtpClient client = new SmtpClient();
            MailMessage message = new MailMessage();

            //设置邮件服务器
            client.Credentials = new NetworkCredential("dp", "1qaz@WSX");
            client.Host = "mail.emstock.com.cn";
            message.From = new MailAddress("dp@emstock.com.cn");
            message.To.Add(to);
            //邮件主题
            message.Subject = "找回密码";

            message.Body = body;
            message.IsBodyHtml = true;
            message.BodyEncoding = Encoding.UTF8;

            try
            {
                //发送邮件
                client.Send(message);
            }
            catch
            {
                throw;
            }
            finally
            {
                //清除发送列表
                message.To.Clear();
            }
        }

        /// <summary>
        /// 读取邮件模板
        /// </summary>
        /// <param name="serverPath"></param>
        /// <returns></returns>
        private string ReadMailTemplate(string serverPath)
        {
            string strHtml = "";
            try
            {
                StreamReader objReader = new StreamReader(serverPath, System.Text.Encoding.GetEncoding("gb2312"));
                strHtml = objReader.ReadToEnd();
                objReader.Close();
            }
            catch
            {
                strHtml = "";
            }
            return strHtml;
        }

        /// <summary>
        /// 生成邮件内容
        /// </summary>
        /// <param name="strOld">参数名集合</param>
        /// <param name="strNew">参数值集合</param>
        /// <returns></returns>
        private string CreateEmail(string strHtml, string[] strOld, string[] strNew)
        {
            for (int i = 0; i < strOld.Length; i++)
            {
                strHtml = strHtml.Replace(strOld[i].ToString(), strNew[i].ToString());
            }
            return strHtml;
        }

        /// <summary>
        /// 修改密码，返回值0：成功1：用户名不存在；2：修改失败
        /// </summary>
        /// <param name="loginName">加密的用户名</param>
        /// <param name="newPassWord">未加密的密码</param>
        /// <returns>0：成功1：用户名不存在</returns>
        public string Update(string loginName, string newPassWord)
        {
            loginName = LYM.CommonWeb.DecryptEncrypt.DESEncrypt.Decrypt(loginName);
            Model.ePioneer_UserInfo model = dal.Login(loginName);
            if (model == null)
                return "1";
            else
            {
                model.PassWord = newPassWord;
                if (Update(model))
                    return "0";
                else
                    return "2";
            }
        }

        /// <summary>
        /// 更新用户姓名和手机号
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="reMyName">用户姓名</param>
        /// <param name="mobile">手机号</param>
        /// <returns></returns>
        public string UpdateUserInfo(string loginName, string reMyName, string mobile)
        {
            Model.ePioneer_UserInfo model = dal.Login(loginName);
            if (model == null)
                return "1";
            else
            {
                model.Mobile = mobile;
                model.UserName = reMyName;
                if (Update(model))
                    return "0";
                else
                    return "2";
            }
        }
        #endregion
    }
}
